package com.cxs.xiaomi.web.controller;

import com.cxs.xiaomi.domain.Address;
import com.cxs.xiaomi.domain.User;
import com.cxs.xiaomi.service.AddressService;
import com.cxs.xiaomi.service.impl.AddressServiceImpl;
import com.cxs.xiaomi.utils.StringUtil;
import cxs.utils.utils.ParamsAsBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/14  16:13
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@WebServlet(name = "AddressServlet", urlPatterns = "/addressServlet")
public class AddressServlet extends BaseServlet {

    private AddressService addressService = new AddressServiceImpl();
    // 获得地址
    public String getAddress(HttpServletRequest request, HttpServletResponse response){
        User user = getUser(request, response);
        if(user != null){
            List<Address> list = addressService.getList(user.getId());
            request.setAttribute("addList",list);
            return "forward:/self_info.jsp";
        }
        return "redirect:/login.jsp";
    }
    // 修改地址
    public String updateAddress(HttpServletRequest request, HttpServletResponse response){
        User user = getUser(request, response);
        if(user != null){
            try {
                Address address = ParamsAsBean.parammeterAsBean(Address.class, request, false);
                if(address != null){
                    address.setUid(user.getId());
                    if(addressService.update(address)){
                        return "redirect:/addressServlet?method=getAddress";
                    }
                    request.setAttribute("msg","地址修改失败");
                    return "forward:/message.jsp";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "redirect:/login.jsp";
    }
    // 删除
    public String deleteAddress(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        if(StringUtil.empty(id)){
            request.setAttribute("msg","id参数必传");
            return "forward:/message.jsp";
        }
        User user = getUser(request, response);
        if(user != null){
            if(addressService.deleteAddr(id.trim())){
                return "redirect:/addressServlet?method=getAddress";
            }
            request.setAttribute("msg","地址删除失败");
            return "forward:/message.jsp";
        }
        return "redirect:/login.jsp";
    }
    // 设为默认地址
    public String defaultAddress(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        if(StringUtil.empty(id)){
            request.setAttribute("msg","id参数必传");
            return "forward:/message.jsp";
        }
        User user = getUser(request, response);
        if(user != null){
            if(addressService.serDefaultAddr(id.trim())){
                return "redirect:/addressServlet?method=getAddress";
            }
            request.setAttribute("msg","默认地址修改失败");
            return "forward:/message.jsp";
        }
        return "redirect:/login.jsp";
    }

    // 添加地址
    public String addAddress(HttpServletRequest request, HttpServletResponse response){
        User user = getUser(request, response);
        if(user != null){
            try {
                Address address = ParamsAsBean.parammeterAsBean(Address.class, request, true);
                address.setUid(user.getId());
                address.setLevel(0);
                if(addressService.addAddr(address)){
                    return "redirect:/addressServlet?method=getAddress";
                }
                request.setAttribute("msg","地址添加失败");
                return "forward:/addressServlet?method=getAddress";
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "redirect:/login.jsp";
    }

    // 测试
    public void test(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("测试方法执行");
    }
}
